What is Request Delegate and its use in ASP.NET Core?

ASP.NET Core के अंतर्गत रिक्वेस्ट डेलीगेट और इसका क्या उपयोग है? दोस्तों!🙏 आइए, इस प्रश्न का उत्तर समझते हैं।

Request Delegate क्या है?

Request Delegate ASP.NET Core में Middleware Pipeline के अंदर एक फ़ंक्शन होता है, जो HTTP Request को प्रोसेस करता है और अगली Middleware को कॉल कर सकता है। यह आमतौर पर HttpContext को इनपुट के रूप में लेता है और Task (Asynchronous Execution) रिटर्न करता है।

Request Delegate के प्रकार

ASP.NET Core में Request Delegate को तीन तरीकों से परिभाषित किया जा सकता है:

  • Anonymous Function (Inline Middleware)
  • Middleware Class
  • Endpoint Routing (Minimal API)

🔍 1. Anonymous Function (Inline Middleware)

आप सीधे Use मेथड में Anonymous Function लिख सकते हैं।

Example: Anonymous Function का उपयोग

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.Use(async (context, next) =>
{
    await context.Response.WriteAsync("Middleware 1 - Before\n");
    await next(); // अगली Middleware को कॉल करना
    await context.Response.WriteAsync("Middleware 1 - After\n");
});

app.Use(async (context, next) =>
{
    await context.Response.WriteAsync("Middleware 2\n");
    await next();
});

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Final Response from Middleware 3\n");
});

app.Run();

Output (Client को Response)

Middleware 1 - Before

Middleware 2

Final Response from Middleware 3

Middleware 1 - After

next() अगले Middleware को कॉल करता है।
Middleware के अंदर Before और After Execution संभव है।

🔍 2. Middleware Class का उपयोग (Custom Middleware)

आप Middleware को अलग क्लास में भी डिफाइन कर सकते हैं।

Step 1: Middleware Class बनाना

public class CustomMiddleware
{
    private readonly RequestDelegate _next;

    public CustomMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        await context.Response.WriteAsync("Custom Middleware Start\n");
        await _next(context); // अगले Middleware को कॉल करना
        await context.Response.WriteAsync("Custom Middleware End\n");
    }
}

Step 2: इसे Pipeline में जोड़ना (Program.cs)

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseMiddleware<CustomMiddleware>();
app.Run(async (context) =>
{
    await context.Response.WriteAsync("Final Response\n");
});
app.Run();

Output

Custom Middleware Start

Final Response

Custom Middleware End

InvokeAsync(HttpContext) का उपयोग Middleware को प्रोसेस करने के लिए किया जाता है।
Middleware का पुन: उपयोग करना आसान हो जाता है।

🔍 3. Endpoint Routing (Minimal API) के साथ Request Delegate

Minimal API में MapGet, MapPost, आदि का उपयोग करके आप Request Delegate को Endpoint के रूप में परिभाषित कर सकते हैं।

Example: Minimal API

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello, World!");
app.MapGet("/hello", async (HttpContext context) =>
{
    await context.Response.WriteAsync("Hello from /hello endpoint");
});
app.Run();

  • यह सीधे RequestDelegate को उपयोग करता है।
  • Routing और Middleware को Combined रूप में उपयोग करने की सुविधा देता है।

🛦 निष्कर्ष

Request Delegate Type कैसे उपयोग करें? कब उपयोग करें?
Anonymous Function app.Use(async (context, next) => { ... }) छोटे Middleware को परिभाषित करने के लिए।
Middleware Class app.UseMiddleware<CustomMiddleware>() Reusable Middleware के लिए।
Minimal API app.MapGet("/", () => "Hello") Routing के साथ Lightweight API के लिए।

💥मुख्य बातें

  1. Request Delegate एक फ़ंक्शन है जो HTTP Request को प्रोसेस करता है।
  2. Middleware में यह अगले Middleware को next() के द्वारा कॉल कर सकता है।
  3. Middleware क्लास में InvokeAsync(HttpContext) इसका एंट्री पॉइंट होता है।
  4. Minimal API में Request Delegate सीधे Routes के रूप में उपयोग किया जाता है।

ASP.NET Core में Middleware Pipeline बनाने के लिए Request Delegate एक महत्वपूर्ण भाग होता है।


Next: क्या हम डेवलपमेंट के दौरान ASP.NET Core में Kestrel सर्वर के अलावा अन्य का उपयोग कर सकते हैं?

टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट

Differences between in-process and out-of-process hosting models

Web Fundamental Concepts in Hindi for Beginners - FAQs with their Answers Part-1

Introduction to ASP.NET Core and Web Frameworks